<template>
  <div class="app-container" v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="标题" prop="title"><el-input v-model="form.title" placeholder="请输入标题" /></el-form-item>
      <el-form-item label="求助电话" prop="helpPhone"><el-input v-model="form.helpPhone" placeholder="请输入求助电话" /></el-form-item>
      <el-form-item label="图片链接A">
        <imageUpload v-model="form.bgImg1" :limit="1" />
        <el-input placeholder="请输选择" v-model="form.briefText3" class="input" :readonly="true">
          <template slot="prepend">
            跳转链接
          </template>
          <template slot="append">
            <div @click="op(3)">选择内容</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="图片链接B">
        <imageUpload v-model="form.bgImg2" :limit="1" />
        <el-input placeholder="请输选择" v-model="form.briefText2" class="input" :readonly="true">
          <template slot="prepend">
            跳转链接
          </template>
          <template slot="append">
            <div @click="op(2)">选择内容</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="图片链接C">
        <imageUpload v-model="form.bgImg3" :limit="1" />
        <el-input placeholder="请输选择" v-model="form.briefText3" class="input" :readonly="true">
          <template slot="prepend">
            跳转链接
          </template>
          <template slot="append">
            <div @click="op(3)">选择内容</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="图片链接D">
        <imageUpload v-model="form.bgImg4" :limit="1" />
        <el-input placeholder="请输选择" v-model="form.briefText4" class="input" :readonly="true">
          <template slot="prepend">
            跳转链接
          </template>
          <template slot="append">
            <div @click="op(4)">选择内容</div>
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer"><el-button type="primary" @click="submitForm">确 定</el-button></div>
    <list v-if="dialog" ref="dialog" @list="getData"></list>
  </div>
</template>
<script>
import { getAboutUs, addAboutUs, updateAboutUs } from '@/api/management/aboutUs';
import list from './list';
export default {
  components: {
    list
  },
  data() {
    return {
      loading: true,
      dialog: false,
      form: {},
      flag: 1,
      rules: {
        title: [{ required: true, message: '标题不能为空' }],
        helpPhone: [{ required: true, message: '请输入求助电话' }],
        browseNum: [{ required: true, message: '点击量不能为空' }],
        content: [{ required: true, message: '详情不能为空' }]
      }
    };
  },
  created() {
    this.loading = true;
    getAboutUs(1).then(response => {
      this.loading = false;
      this.form = response.data;
    });
  },
  methods: {
    op(flag) {
      this.flag = flag;
      this.dialog = true;
      this.$nextTick(() => {
        this.$refs.dialog.init();
      });
    },
    getData(row) {
      if (this.flag === 1) {
        this.form.briefText1 = row.title + '?id=' + row.contentId;
      }
      if (this.flag === 2) {
        this.form.briefText2 = row.title + '?id=' + row.contentId;
      }
      if (this.flag === 3) {
        this.form.briefText3 = row.title + '?id=' + row.contentId;
      }
      if (this.flag === 4) {
        this.form.briefText4 = row.title + '?id=' + row.contentId;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true;
          updateAboutUs(this.form).then(response => {
            this.loading = false;
            this.msgSuccess('修改成功');
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.input {
  width: 40%;
  float: left;
  margin: -150px 0px 0px 200px;
}
</style>
